package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.dao.Company;
import com.example.demo.service.impl.CompanyServiceImpl;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
//@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")  //取日期时使用
//@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")//存日期时使用
@RequestMapping("/company")
public class CompanyController {

    private final CompanyServiceImpl companyService;

    public CompanyController(CompanyServiceImpl companyService) {
        this.companyService = companyService;
    }

    /**
     * 新增公司信息
     *
     * @param company 公司信息对象
     */
    @RequestMapping("/add")
    public String addCompanyInfo(Company company) {

        Boolean addJudge = companyService.addCompanyInfo(company);
        if (addJudge) {
            return "success";
        } else {
            return "error";
        }

    }

    /**
     * 修改企业基本信息
     * 注册日期，企业编码不修改
     *
     * @param company 公司对象
     * @param object  Json格式的对象
     * @return 状态码
     */
    @RequestMapping("/update")
    public String updateCompanyInfo(Company company, @RequestBody JSONObject object) {

        String str = object.toJSONString();
        JSONObject jsonObject = JSON.parseObject(str);
        Company companyBean = new Company();
        companyBean.setCompanyName(jsonObject.getString("companyName"));
        companyBean.setIdType(jsonObject.getString("idType"));
        companyBean.setIdNumber(jsonObject.getString("idNumber"));
        companyBean.setCreateDate(jsonObject.getDate("createDate"));
        companyBean.setRegisterCapital(jsonObject.getDouble("registerCapital"));
        companyBean.setCompanyAddress(jsonObject.getString("companyAddress"));
        companyBean.setEmpNumber(jsonObject.getInteger("empNumber"));
        companyBean.setUpdateDate(new Date());

        Boolean updateJudge = companyService.updateCompanyInfo(companyBean);
        if (updateJudge) {
            return "success";
        } else {
            return "error";
        }
    }

    /**
     * 逻辑删除企业基本信息
     * 只修改is_del属性
     *
     * @param code 企业编码
     * @return 状态码
     */
    @PostMapping("/delete/{code}")
    public String deleteCompanyInfoByCCode(@PathVariable String code) {

        Company company = companyService.selectCompanyInfoByCCode(code);
        company.setIsDel(1);
        Boolean deleteJudge = companyService.updateCompanyInfo(company);

        if (deleteJudge) {
            return "success";
        } else {
            return "error";
        }

    }



    /**
     * 查询全部企业基本信息
     *
     * @return 企业信息列表
     */
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    @ResponseBody
    public List<Company> selectAllCompanyInfo() {

        List<Company> companyList = null;

        try {
            companyList = companyService.selectAllCompanyInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (null != companyList || companyList.size() != 0) {
            return companyList;
        } else {
            return null;
        }
    }

    /**
     * 根据企业编码查询企业详细信息
     *
     * @param code 企业编码
     * @return 企业信息对象
     */
    @RequestMapping("/selectOne/{code}")
    public Company selectCompanyInfoByCCode(@PathVariable  String code) {

        return companyService.selectCompanyInfoByCCode(code);
    }


}
